package com.suning.sawp.in.controller.member;

import java.util.List;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.member.constant.LabelChannelEnum;
import com.suning.sawp.member.service.LabelService;
import com.suning.sawp.member.vo.LabelChannelVo;
import com.suning.sawp.member.vo.LabelVo;

/**
 * 
 * 标签控制层 <br>
 *
 * @author 14060886 祝翔龙
 */
@Controller
@RequestMapping("/adminPage/label")
public class LabelController {

	private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

	@Autowired
	private LabelService labelService;

	@RequestMapping("queryLabelList")
	@ResponseBody
	public ReturnMsg<JSONObject> queryLabelList(@RequestParam(defaultValue = "1", required = false) Integer pageNo) {
		ReturnMsg<JSONObject> result = new ReturnMsg<JSONObject>();
		try {
			result.setData(labelService.queryLabelList(pageNo));
		} catch (Exception e) {
			LOGGER.error("LabelController.queryLabelList error: ", e);
			result.setRetFlag(ReturnMsg.FAIL);
			result.setErrorMessage(e.getMessage());
		}
		return result;
	}

	@RequestMapping("modifyLabelList")
	@ResponseBody
	public ReturnMsg<String> modifyLabelList(@RequestBody LabelVo labelInfo) {
		ReturnMsg<String> result = new ReturnMsg<String>();
		try {
			labelService.modifyLabelList(labelInfo);
		} catch (Exception e) {
			LOGGER.error("LabelController.modifyLabelList error: ", e);
			result.setRetFlag(ReturnMsg.FAIL);
			result.setErrorMessage(e.getMessage());
		}
		return result;
	}

	@RequestMapping("addLabelList")
	@ResponseBody
	public ReturnMsg<String> addLabelList(@RequestBody LabelVo labelInfo) {
		ReturnMsg<String> result = new ReturnMsg<String>();
		try {
			labelService.addLabelList(labelInfo);
		} catch (Exception e) {
			LOGGER.error("LabelController.addLabelList error: ", e);
			result.setRetFlag(ReturnMsg.FAIL);
			result.setErrorMessage(e.getMessage());
		}
		return result;
	}

	@RequestMapping("queryLabelDetailById")
	@ResponseBody
	public ReturnMsg<JSONObject> queryLabelDetailById(String labelId) {
		ReturnMsg<JSONObject> result = new ReturnMsg<JSONObject>();
		try {
			result.setData(labelService.queryLabelDetailById(labelId));
		} catch (Exception e) {
			LOGGER.error("LabelController.queryLabelDetailById error: ", e);
			result.setRetFlag(ReturnMsg.FAIL);
			result.setErrorMessage(e.getMessage());
		}
		return result;
	}

	@RequestMapping("deleteLabelById")
	@ResponseBody
	public ReturnMsg<String> deleteLabelById(String labelId) {
		ReturnMsg<String> result = new ReturnMsg<String>();
		try {
			labelService.deleteLabelById(labelId);
		} catch (Exception e) {
			LOGGER.error("LabelController.deleteLabelById error: ", e);
			result.setRetFlag(ReturnMsg.FAIL);
			result.setErrorMessage(e.getMessage());
		}
		return result;
	}

	@RequestMapping("queryChanelList")
	@ResponseBody
	public ReturnMsg<JSONObject> queryChanelList() {
		ReturnMsg<JSONObject> result = new ReturnMsg<JSONObject>();
		try {
			JSONObject json = new JSONObject();
			json.put("channelList", this.getChannelList());
			result.setData(json);
		} catch (Exception e) {
			LOGGER.error("LabelController.queryChanelList error: ", e);
			result.setRetFlag(ReturnMsg.FAIL);
			result.setErrorMessage(e.getMessage());
		}
		return result;
	}

	private List<LabelChannelVo> getChannelList() {
		List<LabelChannelVo> channelList = Lists.newArrayList();
		for (LabelChannelEnum channelEnum : LabelChannelEnum.values()) {
			channelList.add(new LabelChannelVo(channelEnum.getCode(), channelEnum.getName()));
		}
		return channelList;
	}

}
